package com.ganzi.ganziserver.rabbit.send;

import com.ganzi.ganziserver.core.SendInformation;
import com.ganzi.ganziserver.utils.Topic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendUtil {
    private static Logger log = LoggerFactory.getLogger(SendUtil.class);


    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendMessageToSchool(final SendInformation info)throws Exception{
        CorrelationData correlationData = new CorrelationData(info.getInfoId());
        MessagePostProcessor processor;
        if("-1".equals(info.getType())){
            processor = new MessagePostProcessor(){
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration("5000");
                    message.getMessageProperties().setCorrelationId(info.getInfoId());
                    return message;
                }
            };
        }else{
            processor = new MessagePostProcessor(){
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration("10000");//过期10秒
                    message.getMessageProperties().setCorrelationId(info.getInfoId());
                    return message;
                }
            };
        }
        rabbitTemplate.convertAndSend(Topic.EXCHANGE_FOR_CONTROL, info.getChannelName(), info, processor,correlationData);
//        log.info("发送rabbit消息："+info);
    }

}
